package controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import bean.ProductValue;  

public class Test {

	public static void main(String[] args) {
		// 构建用户geust  
		TestUser guestUser = new TestUser();  
        guestUser.setName("guest");  
        guestUser.setAge(28);  
        // 构建用户root  
        TestUser rootUser = new TestUser();  
        rootUser.setName("root");  
        guestUser.setAge(35);  
        // 构建用户组对象  
        UserGroup group = new UserGroup();  
        group.setName("admin");  
        group.getUsers().add(guestUser);  
        group.getUsers().add(rootUser);  
        // 用户组对象转JSON串  
        String jsonString = JSON.toJSONString(group);  
        System.out.println("jsonString:" + jsonString);  
        // JSON串转用户组对象  
        UserGroup group2 = JSON.parseObject(jsonString, UserGroup.class);  
        System.out.println("group2:" + group2);  
  
        // 构建用户对象数组  
        TestUser[] users = new TestUser[2];  
        users[0] = guestUser;  
        users[1] = rootUser;  
        // 用户对象数组转JSON串  
        String jsonString2 = JSON.toJSONString(users);  
        System.out.println("jsonString2:" + jsonString2);  
        // JSON串转用户对象列表  
        List<TestUser> users2 = JSON.parseArray(jsonString2, TestUser.class);  
        System.out.println("users2:" + users2);
        
        ProductValue pv=new ProductValue(1, "幻影紫", 1);
        ProductValue pv2=new ProductValue(3, "6G", 1);
        ProductValue[] pvs=new ProductValue[2];
        pvs[0]=pv;
        pvs[1]=pv2;
        String jsonString3=JSON.toJSONString(pvs);
        System.out.println("jsonString3:"+jsonString3);
        List<ProductValue> pvs2=JSON.parseArray(jsonString3,ProductValue.class);
        System.out.println(pvs2.get(0).getAttrValue());
        
        Map<Integer,Integer> cart1=new HashMap<Integer, Integer>();
        cart1.put(1, 1);
        cart1.put(2, 2);
        cart1.put(3, 3);
        System.out.println("cart1:");
        for(Map.Entry<Integer, Integer> en:cart1.entrySet()) {
        	System.out.println(en.getKey()+":"+en.getValue());
        }
        
        Map<Integer,Integer> cart2=new HashMap<Integer, Integer>();
        cart2.putAll(cart1);
        cart1.put(2, 0);
        System.out.println("cart2:");
        for(Map.Entry<Integer, Integer> en:cart2.entrySet()) {
        	System.out.println(en.getKey()+":"+en.getValue());
        }
        System.out.println("cart1:");
        for(Map.Entry<Integer, Integer> en:cart1.entrySet()) {
        	System.out.println(en.getKey()+":"+en.getValue());
        }
	}

}

  
  
class TestUser {  
    private String name;  
    private int age;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    @Override  
    public String toString() {  
        return "User [name=" + name + ", age=" + age + "]";  
    }  
};  
  
class UserGroup {  
    private String name;  
    private List<TestUser> users = new ArrayList<TestUser>();  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public List<TestUser> getUsers() {  
        return users;  
    }  
  
    public void setUsers(List<TestUser> users) {  
        this.users = users;  
    }  
  
    @Override  
    public String toString() {  
        return "UserGroup [name=" + name + ", users=" + users + "]";  
    }  
} 